//
//  CBMusicListView.m
//  KangaRoos
//
//  Created by Harvey on 2017/10/6.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "CBMusicListView.h"

#import "MusicManager.h"
#import "Song.h"

@interface CBMusicListView ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>

@property (strong, nonatomic) UIView            *coverView;
@property (strong, nonatomic) UIView            *containView;

@property (strong, nonatomic) UITableView            *tableView;

@property (strong, nonatomic) NSMutableArray            *musicList;


@end

@implementation CBMusicListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        [self initSubView];
        
        [self initData];
    }
    return self;
}

- (instancetype)initWithBlock:(SongClickBlock)block
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        _block = block;
        
        [self initSubView];
        
        [self initData];
    }
    return self;
}

- (void)initSubView
{
    UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    [self addSubview:coverView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgClick)];
    tapGesture.delegate = self;
    [coverView addGestureRecognizer:tapGesture];
    
    UIView *containView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight/3-kTopSafeHeight, kWidth, kHeight*2/3)];
    containView.backgroundColor = [UIColor whiteColor];
    [coverView addSubview:containView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    titleLabel.text = showText(@"音乐列表");
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [containView addSubview:titleLabel];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight*2/3-80) style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = 44;
    tableView.tableFooterView = [UIView new];
    tableView.layer.borderColor = [UIColor blackColor].CGColor;
    tableView.layer.borderWidth = 0.5;
    [containView addSubview:tableView];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight*2/3 - 40, kWidth, 0.5)];
    bottomLine.backgroundColor = [UIColor blackColor];
    [containView addSubview:bottomLine];
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, kHeight*2/3 - 40, kWidth, 40)];
    [closeBtn setTitle:showText(@"关闭") forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [containView addSubview:closeBtn];
}

- (void)initData
{    
    MusicManager *musicManager = [MusicManager share];
    //获取歌曲列表
    [musicManager loadMusic];
    self.musicList = musicManager.songs;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    //Tip:我们可以通过打印touch.view来看看具体点击的view是具体是什么名称,像点击UITableViewCell时响应的View则是UITableViewCellContentView.
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        //返回为NO则屏蔽手势事件
        return NO;
    }
    return YES;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musicList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"musicCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    Song *song = self.musicList[indexPath.row];
    cell.textLabel.text = song.songTitle;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Song *song = self.musicList[indexPath.row];
    if (_block) {
        _block(song);
    }
    [self hide];
}

#pragma mark - btn click events
- (void)bgClick
{
    [self hide];
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)hide
{
    [self removeFromSuperview];
}

@end
